package com.adb.receivecontent.utility;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

import com.adb.receivecontent.bean.LinkMagnetBean;
import com.adb.receivecontent.bean.LinkParam;


public class HttpUploadFile {
		
	@SuppressLint("NewApi")
	public String postImageIMEI(Context mContext,String vLocalPath,LinkMagnetBean vBeanApdater) {
		String message = "";
	    // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost(Const.UPLOAD_OPERATION);
	    ArrayList<LinkParam> mParamArrayList;
	    try {
	    	
	    	 Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(new File(vLocalPath)));   
	         ByteArrayOutputStream stream = new ByteArrayOutputStream();
	         bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
	         byte [] byte_arr = stream.toByteArray();
	         String image_str = Base64.encodeToString(byte_arr,Base64.DEFAULT);
	    	
	         
	         
	        // Add your data
	        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	        
	        nameValuePairs.add(new BasicNameValuePair(vBeanApdater.getStrFileName(),image_str));
	        
	        
	        mParamArrayList = vBeanApdater.getmArrayListParams();
	        if(mParamArrayList != null){
	        
	        	for(int i =0;i<mParamArrayList.size();i++){
	        		
	        		LinkParam mTempParams = mParamArrayList.get(i);
	        		nameValuePairs.add(new BasicNameValuePair(mTempParams.getStrKey(),mTempParams.getStrValue()));	
		        }
	        }
	        
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        message = convertStreamToString(response.getEntity().getContent());
	        Log.d("Http Response:", message);
	        return message;
	        
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return message;
        
	} 
	
	private String convertStreamToString(InputStream is) {
		String line = "";
		StringBuilder total = new StringBuilder();
		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		try {
			while ((line = rd.readLine()) != null) {
				total.append(line);
			}
		} catch (Exception e) {

		}
		return total.toString();
	}

}
